/*
 * @Copyright: EasyCompany 2021
 * @Author: asterisk
 * @Date: 2021-03-15 16:15:30
 * @Description: 
 */

import '../../easy_log.dart';
import '../utils.dart';

class EasyLogConsoleAppender implements EasyLogAppender {
  EasyLoggerConfig _config; //ref;
  final PrintLogCallback printLog;
  EasyLogConsoleAppender(this._config, {this.printLog = print});
  @override
  void logo(dynamic message) {
    if (message is EasyLogMessage) {
      String content =
          _config.formatters['EasyLogMessageFormatter'].format(message);
      logs(content);
    } else if (message is EasyLogInfo) {
      List<dynamic> contents =
          _config.formatters['EasyLogInfoFormatter'].format(message);
      List<EasyLogMessage> messages = [];
      for (var content in contents) {
        messages.add(EasyLogMessage(
            tags: {
              'appName': EasyLoggerFactory.appName,
              'logName': _config.logName.isEmpty
                  ? EasyLoggerFactory.appName
                  : _config.logName,
            },
            timestamp: fromMillisecondsSinceEpoch(),
            level: EasyLogLevel.FLAG,
            message: content));
      }
      for (var message in messages) {
        String content =
            _config.formatters['EasyLogMessageFormatter'].format(message);
        logs(content);
      }
    }
  }

  @override
  void logs(String message) {
    printLog(message);
  }

  @override
  Future init() {
    return null;
  }

  @override
  Future unInit() {
    return null;
  }
}
